//
//  STTools_GxTiShiAlertView.m
//  togetherPlay
//
//  Created by coder on 2019/3/27.
//  Copyright © 2019 stoneobs.qq.com. All rights reserved.
//

#import "STTools_GxTiShiAlertView.h"
@interface STTools_GxTiShiAlertView()
@property(nonatomic, strong) NSString                     *content;/**<  */
@property(nonatomic, strong) NSString                     *gxlianjieUrl;/**<  */
@property(nonatomic, assign) BOOL                     isqzgx;/**<  */

@property(nonatomic, strong) STButton                     *clearButton;
@property(nonatomic, strong) UIView                     *customView;
@end
@implementation STTools_GxTiShiAlertView
+ (void)stoneobs_fun_showGxNeiRongViewWithContent:(NSString*)content isqzgx:(BOOL)isqzgx gxlianjieUrl:(NSString*)gxlianjieUrl{
    STTools_GxTiShiAlertView * stoneobs_temp_view = [[STTools_GxTiShiAlertView alloc] initWithFrame:UIScreen.mainScreen.bounds];
    stoneobs_temp_view.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0.4];
    [UIApplication.sharedApplication.delegate.window addSubview:stoneobs_temp_view];
    stoneobs_temp_view.content = content;
    stoneobs_temp_view.gxlianjieUrl = gxlianjieUrl;
    stoneobs_temp_view.isqzgx = isqzgx;
    [stoneobs_temp_view stoneobs_ui_configSubView];
    
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
}
#pragma mark --subView
- (void)stoneobs_ui_configSubView{
    __weak typeof(self) weakSelf =  self;
    self.clearButton = [[STButton alloc] initWithFrame:self.bounds
                                                 title:nil
                                            titleColor:nil
                                             titleFont:0
                                          cornerRadius:0
                                       backgroundColor:[[UIColor blackColor] colorWithAlphaComponent:0.4]
                                       backgroundImage:nil
                                                 image:nil];
    [self.clearButton stoneobs_fun_touch_handle:^(UIButton *sender) {
        if (!weakSelf.isqzgx) {
            [weakSelf dismiss];
        }
    }];
    [self addSubview:self.clearButton];
    
    self.customView = [[UIView alloc] initWithFrame:CGRectMake(30, STONEOBS_UI_UISCREEN_HEIGHT, STONEOBS_UI_UISCREEN_WIDTH - 60, 100)];
    self.customView.centerX = STONEOBS_UI_UISCREEN_WIDTH / 2;
    self.customView.top = STONEOBS_UI_UISCREEN_HEIGHT;
    self.customView.backgroundColor = [UIColor clearColor];
    self.customView.layer.cornerRadius = 10;
    self.customView.clipsToBounds = YES;
    self.customView.centerY = STONEOBS_UI_UISCREEN_HEIGHT / 2;
    [self addSubview:self.customView];
    

    UIImage *stoneobs_temp_image = [UIImage imageNamed:@"happy_game_wh_update_bg"];
    
    UIImageView * stoneobs_temp_imageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.customView.width, self.customView.width *1.44)];
    stoneobs_temp_imageview.image = stoneobs_temp_image;
    //    imageview.image = [image resizableImageWithCapInsets:UIEdgeInsetsMake(175, 0, 0, 0) resizingMode:UIImageResizingModeStretch];
    stoneobs_temp_imageview.userInteractionEnabled = YES;
    [self.customView addSubview:stoneobs_temp_imageview];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, stoneobs_temp_imageview.height * 0.4, self.customView.width, 44)
                                                     text:@"更新内容"
                                                textColor:UIColor.blackColor
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    [stoneobs_temp_imageview addSubview:titleLable];
    
    UIScrollView * scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, titleLable.bottom, self.customView.width - 20, stoneobs_temp_imageview.height - 20 - titleLable.bottom - 45)];
    if (@available(iOS 11.0, *)) {
        [scrollView setContentInsetAdjustmentBehavior:@(2)];
    } else {
        // Fallback on earlier versions
    }
    [stoneobs_temp_imageview addSubview:scrollView];
    
    STLabel * stitleLable = [[STLabel alloc] initWithFrame:CGRectMake(10, 0, scrollView.width - 20, 30)
                                                      text:self.content
                                                 textColor:WH_UICOLOR_FROM_RGBA(0x333333)
                                                      font:14
                                               isSizetoFit:NO
                                             textAlignment:NSTextAlignmentLeft];
    [scrollView addSubview:stitleLable];
    [stitleLable stoneobs_fun_st_autoadjustTextHeight];
    //    [NSString stoneobs_fun_lableAutoAdjustheightWithLabel:stitleLable];
    //    CGSize size = [titleLable sizeThatFits:CGSizeMake(stitleLable.width, CGFLOAT_MAX)];
    //    stitleLable.height = size.height;
    scrollView.contentSize = CGSizeMake(0, stitleLable.bottom + 10);
    
    
    STButton * cancleButton = [[STButton alloc] initWithFrame:CGRectMake(0, scrollView.bottom + 30, 80, 40)
                                                        title:@"取消"
                                                   titleColor:UIColor.whiteColor
                                                    titleFont:15
                                                 cornerRadius:20
                                              backgroundColor:[UIColor.redColor colorWithAlphaComponent:0.4]
                                              backgroundImage:nil
                                                        image:nil];
    cancleButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [cancleButton stoneobs_fun_touch_handle:^(UIButton *sender) {
        [weakSelf dismiss];
    }];
    cancleButton.right = self.customView.width / 2 - 30;
    cancleButton.bottom = stoneobs_temp_imageview.height - 10;
    [stoneobs_temp_imageview addSubview:cancleButton];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(0, stitleLable.bottom + 30, 80, 40)
                                                        title:@"确认"
                                                   titleColor:UIColor.whiteColor
                                                    titleFont:15
                                                 cornerRadius:20
                                              backgroundColor:[UIColor.orangeColor colorWithAlphaComponent:0.5]
                                              backgroundImage:nil
                                                        image:nil];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    
    [confimButton stoneobs_fun_touch_handle:^(UIButton *sender) {
        [UIView animateWithDuration:0.25 animations:^{
            UIApplication.sharedApplication.delegate.window.alpha = 0.1;
        } completion:^(BOOL finished) {
            if ([UIApplication.sharedApplication canOpenURL:self.gxlianjieUrl.mj_url]) {
                    [UIApplication.sharedApplication openURL:[NSURL URLWithString:self.gxlianjieUrl]];
                        exit(0);
            }
        }];
    }];
    confimButton.left = self.customView.width / 2 + 30;
    confimButton.bottom = stoneobs_temp_imageview.height - 10;
    [stoneobs_temp_imageview addSubview:confimButton];
    
    
    stoneobs_temp_imageview.userInteractionEnabled = YES;
    self.customView.height = stoneobs_temp_imageview.bottom ;
    if (self.isqzgx) {
        cancleButton.hidden = YES;
        confimButton.centerX = self.customView.width / 2;
    }
    
    self.customView.top = STONEOBS_UI_UISCREEN_HEIGHT;
    
    [self show];
}


#pragma mark --Private Method
- (void)show{
    [UIView animateWithDuration:0.6
                          delay:0
         usingSpringWithDamping:0.6
          initialSpringVelocity:0.7
                        options:UIViewAnimationOptionLayoutSubviews
                     animations:^{
                         self.customView.centerY = STONEOBS_UI_UISCREEN_HEIGHT / 2;
                     } completion:^(BOOL finished) {
                         
                     }];
}
- (void)dismiss{
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = STONEOBS_UI_UISCREEN_HEIGHT;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (void)dismissWithHandle:(void(^)())handle{
    [UIView animateWithDuration:0.25 animations:^{
        self.customView.top = STONEOBS_UI_UISCREEN_HEIGHT;
        self.clearButton.alpha = 0;
    } completion:^(BOOL finished) {
         [self removeFromSuperview];
    }];
}
@end
